package com.lc.baselib.net.manager.Impl;

import android.app.Application;

import com.lc.baselib.net.manager.IRepositoryManager;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.Lazy;
import retrofit2.Retrofit;

/**
 * @author LiuChao
 * @describe
 * @date 2018/6/25
 * @contact email:450127106@qq.com
 */
public class RepositoryManagerImpl implements IRepositoryManager {
    private Retrofit mRetrofit;
    //private Lazy<RxCache> mRxCache;
    private Application mApplication;
    private final Map<String, Object> mRetrofitServiceCache = new HashMap<>();
    private final Map<String, Object> mCacheServiceCache = new HashMap<>();

    public RepositoryManagerImpl(Retrofit retrofit/*, Lazy<RxCache> rxCache*/, Application application) {
        this.mRetrofit = retrofit;
        //this.mRxCache = rxCache;
        this.mApplication = application;
    }

    /**
     * 根据传入的Class获取对应的Retrift service
     *
     * @param service
     * @param <T>
     * @return
     */
    @Override
    public <T> T obtainRetrofitService(Class<T> service) {
        T retrofitService;
        synchronized (mRetrofitServiceCache) {
            retrofitService = (T) mRetrofitServiceCache.get(service.getName());
            if (retrofitService == null) {
                retrofitService = mRetrofit.create(service);
                mRetrofitServiceCache.put(service.getName(), retrofitService);
            }
        }
        return retrofitService;
    }

    /**
     * 根据传入的Class获取对应的RxCache service
     *
     * @param cache
     * @param <T>
     * @return
     */
    @Override
    public <T> T obtainCacheService(Class<T> cache) {
      /*  T cacheService;
        synchronized (mCacheServiceCache) {
            cacheService = (T) mCacheServiceCache.get(cache.getName());
            if (cacheService == null) {
                cacheService = mRxCache.get().using(cache);
                mCacheServiceCache.put(cache.getName(), cacheService);
            }
        }
        return cacheService;*/
        return null;
    }
}
